package com.isoft.controller;

import com.google.code.kaptcha.Producer;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 验证码生成
 */
@Controller
public class VerifyCodeController {
    @Autowired
    private Producer producer;
    @RequestMapping("getVerifyCode")
    public void geVerifyCode(HttpServletResponse response, HttpSession session) throws IOException {
        String textCode = producer.createText();
//        存储验证码，session或者是redis
//      存储在session中
        session.setAttribute("verifyCode", textCode);
        System.out.println("session.textcode=" + textCode);
        BufferedImage bi = producer.createImage(textCode);
        response.setContentType("image/png");
        ServletOutputStream so = response.getOutputStream();
        ImageIO.write(bi, "png", so);
    }
}